package com.wn.androidflutterinnerwndemo

import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import io.flutter.embedding.android.FlutterFragment
import io.flutter.embedding.android.RenderMode
import io.flutter.embedding.android.TransparencyMode

/**
 * Author : wn
 * Email : maoning20080808@163.com
 * Date : 2025/7/26 9:58
 * Description : 使用不同引擎加载页面
 */
class WithEngineActivity : AppCompatActivity(){


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.with_engine_main)
        addFlutterFragment()
        addFlutterFragment2()
    }

    private fun addFlutterFragment2() {
        // 使用缓存引擎创建 FlutterFragment
        val flutterFragment = FlutterFragment.withCachedEngine("wn_engine_id")
            .renderMode(RenderMode.texture) // 或 RenderMode.surface
            .transparencyMode(TransparencyMode.transparent) // 可选
            .shouldAttachEngineToActivity(true) // 可选
            .build<FlutterFragment>()

        // 将FlutterFragment添加到Activity
        supportFragmentManager
            .beginTransaction()
            .add(R.id.main_fragment_container2, flutterFragment, TAG_FLUTTER_FRAGMENT)
            .commit()
    }

    /**
     * 使用withNewEngine ，没有都要加载，显示很慢。
     */
    private fun addFlutterFragment() {
        // 创建FlutterFragment
        val flutterFragment: FlutterFragment = FlutterFragment.withNewEngine()
            .initialRoute("/main") // 设置初始路由
            .build()

        // 将FlutterFragment添加到Activity
        supportFragmentManager
            .beginTransaction()
            .add(R.id.main_fragment_container, flutterFragment, TAG_FLUTTER_FRAGMENT)
            .commit()
    }

    companion object {
        const val TAG_FLUTTER_FRAGMENT: String = "flutter_fragment"
    }


}